package com.example.administrator.uitest.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.uitest.R;
import com.example.administrator.uitest.adapter.MineAdapter;
import com.example.administrator.uitest.intface.DoView;
import com.example.administrator.uitest.request.MineList_Request;
import com.example.administrator.uitest.request.UserInfoChange_Request;
import com.example.administrator.uitest.tool.UserTool;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * Created by Maker on 2019/4/15.
 */

public class MineActivity extends Activity {
    private ListView listView;
    private TextView user_id;
    private EditText user_Name,user_Pwd;
    private Button set,outLogin;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_activity);
        initView();
        initData();
    }

    private void initData() {
        user_id.setText(UserTool.userId);
        user_Pwd.setText(UserTool.userPwd);
        user_Name.setText(UserTool.userName);
        refershList();
        set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String uPwd = "";
                String uName= "";
                if(user_Name.getText().toString()!=null&&user_Pwd.getText().toString()!=null){
                    try {
                        uName = URLEncoder.encode(user_Name.getText().toString(),"utf-8");
                        uPwd = URLEncoder.encode(user_Pwd.getText().toString(),"utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(MineActivity.this, "请填写正确的信息", Toast.LENGTH_SHORT).show();

                }
                String params = "userId="+UserTool.userId+"&userPwd="+uPwd+"&userName="+uName+"&userSgin=0";
                UserInfoChange_Request request = new UserInfoChange_Request(MineActivity.this,params);
                request.conn(new DoView() {
                    @Override
                    public void setSuccessfulView(Object object) {
                        if (object.toString().equals("true")){
                            Toast.makeText(MineActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
                            final SharedPreferences.Editor editor = sharedPreferences.edit();
                            //获取用户账户和密码
                            editor.putString("pwd",user_Pwd.getText().toString());
                            editor.putString("name",user_Name.getText().toString());
                            editor.commit();
                            UserTool.userName = user_Name.getText().toString();
                            UserTool.userPwd = user_Pwd.getText().toString();

                        }else {
                            Toast.makeText(MineActivity.this, "修改失败", Toast.LENGTH_SHORT).show();

                        }
                    }

                    @Override
                    public void setFiledView(Object object) {
                        Toast.makeText(MineActivity.this, "出错啦", Toast.LENGTH_SHORT).show();

                    }
                });
            }
        });
    }

    private void refershList() {
        String params = "userId="+UserTool.userId;
        MineList_Request request = new MineList_Request(MineActivity.this,params);
        request.conn(new DoView() {
            @Override
            public void setSuccessfulView(Object object) {
                List<Map<String,String>> data = (List<Map<String, String>>) object;
                listView.setAdapter(new MineAdapter(data,MineActivity.this));
            }

            @Override
            public void setFiledView(Object object) {

            }
        });
        outLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.clear();
                editor.commit();
                startActivity(new Intent(MineActivity.this,LoginActivity.class));
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(MineActivity.this,HomeActivity.class));
        finish();
    }

    private void initView() {
        listView = findViewById(R.id.list);
        user_id = findViewById(R.id.user);
        user_Name = findViewById(R.id.name);
        user_Pwd = findViewById(R.id.pwd);
        set = findViewById(R.id.set);
        outLogin = findViewById(R.id.outLogin);
    }
}
